package com.eastfair.questionnaire.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.questionnaire.dao.AnswerResultMapper;
import com.eastfair.questionnaire.dto.AnswerResultDTO;
import com.eastfair.questionnaire.entity.AnswerResult;
import com.eastfair.questionnaire.service.AnswerResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 答题结果
 * </p>
 *
 * @author yuwx
 * @date 2021-08-18
 */
@Slf4j
@Service
public class AnswerResultServiceImpl extends SuperServiceImpl<AnswerResultMapper, AnswerResult> implements AnswerResultService {

    private ProjectCoreServiceFeign projectCoreServiceFeign;

    @Autowired
    public AnswerResultServiceImpl(ProjectCoreServiceFeign projectCoreServiceFeign) {
        this.projectCoreServiceFeign = projectCoreServiceFeign;
    }

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(AnswerResult entity) {
        UpdateWrapper<AnswerResult> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(AnswerResult::getProjectId, entity.getProjectId()).eq(AnswerResult::getId, entity.getId());
        entity.setProjectId(null);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(AnswerResult model) {
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(ContextUtil.getProjectId()));
        model.setProjectId(ContextUtil.getProjectId());
        return R.successDef();
    }


    @Override
    public List<AnswerResult> listByQuestion(Long projectId, List<Long> questionIdList, String subjectType, Long subjectId) {
        if (Objects.isNull(projectId)
                || Objects.isNull(subjectId)
                || StringUtils.isBlank(subjectType)) {
            return Collections.emptyList();
        }
        if (CollectionUtil.isEmpty(questionIdList)) {
            return Collections.emptyList();
        }
        QueryWrapper<AnswerResult> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(AnswerResult::getProjectId, projectId)
                .in(AnswerResult::getQuestionId, questionIdList)
                .eq(AnswerResult::getSubjectType, subjectType)
                .eq(AnswerResult::getSubjectId, subjectId)
                .eq(AnswerResult::getIsEnable, BusinessConstant.ENABLE_YES)
                .eq(AnswerResult::getIsDelete, BusinessConstant.DELETE_NO);
        List<AnswerResult> answerResultList = list(queryWrapper);
        return CollectionUtil.isEmpty(answerResultList) ? Collections.emptyList() : answerResultList;
    }

    @Override
    public List<AnswerResult> listByQuestion(Long projectId, Long questionId, String subjectType, Long subjectId) {
        if (Objects.isNull(projectId)
                || Objects.isNull(questionId)
                || Objects.isNull(subjectId)
                || StringUtils.isBlank(subjectType)) {
            return Collections.emptyList();
        }
        QueryWrapper<AnswerResult> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(AnswerResult::getProjectId, projectId)
                .eq(AnswerResult::getQuestionId, questionId)
                .eq(AnswerResult::getSubjectType, subjectType)
                .eq(AnswerResult::getSubjectId, subjectId)
                .eq(AnswerResult::getIsEnable, BusinessConstant.ENABLE_YES)
                .eq(AnswerResult::getIsDelete, BusinessConstant.DELETE_NO);
        List<AnswerResult> answerResultList = list(queryWrapper);
        return CollectionUtil.isEmpty(answerResultList) ? Collections.emptyList() : answerResultList;
    }

    @Override
    @Transactional
    public void save(Long projectId, String subjectType, Long subjectId, List<AnswerResultDTO> answerResultList) {
        if (Objects.isNull(projectId)
                || StringUtils.isBlank(subjectType)
        ) {
            throw new NullPointerException(projectId + ":" + subjectType + ":" + subjectId);
        }

        UpdateWrapper<AnswerResult> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(AnswerResult::getProjectId, projectId)
                .eq(AnswerResult::getSubjectId, subjectId)
                .eq(AnswerResult::getSubjectType, subjectType);
        AnswerResult answerResult = AnswerResult.builder().isDelete(BusinessConstant.DELETE_YES).build();
        SqlHelper.retBool(this.getBaseMapper().update(answerResult, updateWrapper));

        if (CollectionUtil.isEmpty(answerResultList)) {
            return;
        }
        List<AnswerResult> saveAnswerResultList = ConvertUtil.convertList(answerResultList, AnswerResult.class);
        saveAnswerResultList.stream().forEach(saveAnswerResult -> {
            saveAnswerResult.setId(DefaultUidGenerator.getUidOfProjectId(projectId));
            saveAnswerResult.setProjectId(projectId);
            saveAnswerResult.setSubjectType(subjectType);
            saveAnswerResult.setSubjectId(subjectId);
        });
        saveBatch(saveAnswerResultList);
    }


}
